const mongoose = require("mongoose");
const ordersModel = mongoose.model("orders");

module.exports = {

        add: async (order) => await ordersModel.create(order),
        delete: async (_id) =>
                await ordersModel.deleteOne({ _id }),
        getAll: async () => {
                // 获取所有订单信息，populate为链式调用方法
                return await ordersModel.find().populate(`userId`).populate(`articleId`).populate(`sell`);
        },
        // 收藏夹专用接口
        getFavorite:async ()=>{
                // 获取所有订单信息，populate为链式调用方法
              return await ordersModel.find().populate([
                'userId', 'sell',{
                        // 链式调用，进行多表查询
                        path: 'articleId',
                        populate: {
                                path: 'userId'
                        }
                }
        ]);
        },
        // 带参进入查找符合条件的所有信息
        findId: async (userId) => { return await ordersModel.find({ userId }).sort({_id:-1}).populate(`userId`).populate(`articleId`).populate(`sell`) }
        // 带入商品ID查找该商品绑定的卖家以及买家信息
        ,
        findArtId: async (articleId) => { return await ordersModel.find({ articleId }).populate(`userId`).populate(`articleId`).populate(`sell`) }
}
